package com.work.rcj.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.work.rcj.common.base.BaseController;
import com.work.rcj.common.exception.BusinessException;
import com.work.rcj.common.model.Response;
import com.work.rcj.common.model.page.PageResult;
import com.work.rcj.entity.OldPeople;
import com.work.rcj.pojo.dto.OldPeopleDto;
import com.work.rcj.pojo.vo.OldPeopleVo;
import com.work.rcj.service.OldPeopleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 老年人管理
 *
 * @author rcj
 * @since 2022-02-26
 */
@RestController
@RequestMapping("/oldPeople")
public class OldPeopleController extends BaseController {

    @Autowired
    private OldPeopleService oldPeopleService;


    /**
     * 老年人列表
     *
     * @param dto
     * @return
     */
    @GetMapping("/list")
    public Response<PageResult<OldPeopleVo>> oldPeopleList(OldPeopleDto dto) {
        return Response.success(getDataTable(oldPeopleService.oldPeopleList(dto)));
    }

    /**
     * 新增老年人
     *
     * @param req
     * @return
     */
    @PostMapping("/save")
    public Response save(@RequestBody OldPeople req) {
        QueryWrapper<OldPeople> wrapper = new QueryWrapper<>();
        wrapper.eq("id_card", req.getIdCard());
        wrapper.eq("deleted", 0);
        OldPeople oldPeople = oldPeopleService.getOne(wrapper);
        if (oldPeople != null) {
            throw new BusinessException("500002", "该身份证号已存在已存在");
        }
        oldPeopleService.save(req);
        return Response.success();
    }

    /**
     * 修改老年人
     *
     * @param req
     * @return
     */
    @PostMapping("/update")
    public Response update(@RequestBody OldPeople req) {
        QueryWrapper<OldPeople> wrapper = new QueryWrapper<>();
        wrapper.eq("id_card", req.getIdCard());
        wrapper.eq("deleted", 0);
        wrapper.ne("id",req.getId());
        OldPeople oldPeople = oldPeopleService.getOne(wrapper);
        if (oldPeople != null) {
            throw new BusinessException("500002", "该身份证号已存在已存在");
        }
        UpdateWrapper<OldPeople> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", req.getId());
        oldPeopleService.update(req, updateWrapper);
        return Response.success();
    }

    /**
     * 删除老年人
     *
     * @param oldPeople
     * @return
     */
    @PostMapping("/del")
    public Response del(@RequestBody OldPeople oldPeople) {
        OldPeople entity = new OldPeople();
        entity.setId(oldPeople.getId());
        entity.setDeleted("1");
        oldPeopleService.updateById(entity);
        return Response.success();
    }

    /**
     * 老年人详情
     *
     * @param id
     * @return
     */
    @GetMapping("/detail")
    public Response<OldPeople> detail(Long id) {
        OldPeople oldPeople = oldPeopleService.getById(id);
        return Response.success(oldPeople);
    }

}



